package com.example.mscluster.provider.user;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author sucz
 * @create 2019/12/10 19:29
 */
@Component
public class RedisUtils {

//    @Autowired
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    public static String MS_PREFIX_KEY = "mscluster_";

    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 写入缓存并设置时效
     * @param key
     * @param value
     * @param expireTime
     * @return
     */
    public boolean set(String key, Object value, long expireTime) {
        try {
            if (expireTime > 0) {
                redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 批量删除
     * @param keys
     */
    public void remove(String...keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 删除
     * @param key
     */
    private void remove(String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否存在对此key
     * @param key
     */
    public boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }


    /**
     * 设置缓存时效
     * @param key
     */
    public boolean expireTime(String key,Long expireTime) {
        try {
            redisTemplate.expire(key,expireTime,TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取缓存时效
     * @param key
     */
    public Long getExpire(String key) {
        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key,redisTemplate.getConnectionFactory());
        Long time = entityIdCounter.getExpire();
        return time;
    }

    /**
     * 根据key获取
     * @param key
     * @return
     */
    public Object get(String key) {
        return null == key ? null : redisTemplate.opsForValue().get(key);
    }

    /**
     * 写入List
     * @param key
     * @param value
     * @return
     */
    public boolean pushToList(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 写入List,有时效
     * @param key
     * @param value
     * @param expireTime
     * @return
     */
    public boolean pushToList(String key, Object value, long expireTime) {
        try {
            if (expireTime > 0) {
                redisTemplate.opsForList().rightPush(key, value);
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            } else {
                redisTemplate.opsForList().rightPush(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取List
     * @param key
     * @param start
     * @param end
     * @return
     */
    public List<Object> getList(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 写入Map
     * @param key
     * @param map
     * @return
     */
    public boolean setMap(String key, Map<String, Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 写入Map,有时效
     * @param key
     * @param map
     * @param expireTime
     * @return
     */
    public boolean setMap(String key, Map<String, Object> map, long expireTime) {
        try {
            if (expireTime > 0) {
                redisTemplate.opsForHash().putAll(key, map);
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            } else {
                redisTemplate.opsForHash().putAll(key, map);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取Map
     * @param key
     * @return
     */
    public Map<Object, Object> getMap(String key) {
        try {
            return redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
